<?php
/**
 * Description: Include All functions support for Post types .
 * Version: 0.1
 * Author: zonthemes
 * Author URI: http://zonthemes.com/
 */

//add thumbnail images to post type.
add_theme_support( 'post-thumbnails', array( 'agents', 'property', 'post', 'features' ) );

//functions cat string.
function zonthemes_cat_string($string,$number){
    $tmp = explode(" ",$string);
    $length = count($tmp);
    $result = "";
    for($i=0;$i<=$number;$i++){
        $result = $result . " ". $tmp[$i];
    }
    if($length > $i){
        echo $result.' ...';
    }else{
        echo $result;
    }
}
//end cat string.

//function get thumbnail by size
function zon_get_thumbnail($size){
    global $image_url, $image_id, $post;
    if(has_post_thumbnail()){
        $image_id = get_post_thumbnail_id();
        $image_url = wp_get_attachment_image_src($image_id,$size);
        $image_url = $image_url[0];
    } else {
        $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID );
        $attachments = get_posts( $args );
        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                $image_url = wp_get_attachment_image_src($attachment->ID, $size);
                $image_url = $image_url[0];
            }
        }
    }
    return $image_url;
}
//end get_thumbnail

function zon_meta($name){
  global $post;
  $value = get_post_meta($post->ID, $name, true); 
  return $value;
}

function zon_options($name,$note = null){
  global $data;
  if(isset($data[$name]) and $data[$name] != ""){
    $value = $data[$name];
    return $value;
  }
  else{
    return $note;
  }
}

//functions views images in gallery upload.
function zonthemes_display_gallery( $post_id, $number, $start_tag, $end_tag, $img_size, $no_images = null) {
  $args = array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'post_parent' => $post_id,
    'post_mime_type' => 'image',
    'posts_per_page' => $number,
    'order' => 'ASC',
    'orderby' => 'menu_order',
  );
  $args = apply_filters( 'be_gallery_metabox_args', $args );
  $loop = get_posts( $args );
  if( !$loop ) :
    echo $no_images;
  else :
  foreach( $loop as $key => $img ):
    $src = wp_get_attachment_image_src( $img->ID, apply_filters( 'be_gallery_metabox_image_size', $img_size ) );
    echo $start_tag;
    echo '<img src="'.$src[0].'" alt="'.$img->post_title.'" title="'.$img->post_title.'" />';
    echo $end_tag;
  endforeach;
  endif;
}
//end display gallery.

//function convert string to number.
function zonthemes_convert($s)
{
    // convert "," to "."
    $s = str_replace(',', '.', $s);

    // remove everything except numbers and dot "."
    $s = preg_replace("/[^0-9\.]/", "", $s);

    // remove all seperators from first part and keep the end
    $s = str_replace('.', '',substr($s, 0, -3)) . substr($s, -3);

    // return float
    return (float) $s;
}
function zonthemes_convert_save($s)
{
    // convert "," to "."
    $s = str_replace(',', '', $s);

    // remove everything except numbers and dot "."
    $s = preg_replace("/[^0-9\.]/", "", $s);

    // return float
    return (float) $s;
}

/*function pagenavi*/
function zonthemes_pagenavi() { ?>
<div class="p-navi">
<ul>
    <?php
        global $wp_query;
        $big = 999999999;
        $args = array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?page=%#%',
            'total' => $wp_query->max_num_pages,
            'current' => max( 1, get_query_var( 'paged') ),
            'show_all' => false,
            'end_size' => 3,
            'mid_size' => 2,
            'prev_next' => True,
            'prev_text' => __('Previous'),
            'next_text' => __('Next'),
            'type' => 'list',
            );
        echo paginate_links($args);
    ?>
    </ul>
</div>
<?php }


/*function breadcrumb*/
function zonthemes_breadcrumbs() {  
  global $data;
  /* === OPTIONS === */
  $home = $data['zon_option_bread_home'];
  if($sep){
    $text['home'] = $home;
  }else{
    $text['home']   = 'Home';
  }
  $cate = $data['zon_option_bread_cate'];
  if($cate){
    $text['category'] = $cate;
  }else{
    $text['category'] = 'Archive by Category : %s';
  }
  $search = $data['zon_option_bread_search'];
  if($search){
    $text['search'] = $search;
  }else{
    $text['search']  = 'Search Results for : %s';
  }
  $tag = $data['zon_option_bread_tag'];
  if($tag){
    $text['tag'] = $tag;
  }else{
    $text['tag']  = 'Posts Tagged : %s';
  }
  $author = $data['zon_option_bread_author'];
  if($tag){
    $text['author'] = $author;
  }else{
    $text['author']  = 'Articles Posted by : %s';
  }
  $error = $data['zon_option_bread_error'];
  if($tag){
    $text['404'] = $error;
  }else{
    $text['404']  = 'Error 404';
  }

  $sep = $data['zon_option_bread_sep'];

  $show_current  = 1; // 1 - show current post/page/category title in breadcrumbs, 0 - don't show  
  $show_on_home  = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show  
  $show_home_link = 1; // 1 - show the 'Home' link, 0 - don't show  
  $show_title   = 1; // 1 - show the title for the links, 0 - don't show  
  if($sep){
    $delimiter = $sep;
  }else{
    $delimiter   = ' &raquo; ';  
  } 
  $before     = '<span class="current">'; // tag before the current crumb  
  $after     = '</span>'; // tag after the current crumb  
  /* === END OF OPTIONS === */  
  
  global $post;  
  $home_link  = home_url('/');  
  $link_before = '<span typeof="v:Breadcrumb">';  
  $link_after  = '</span>';  
  $link_attr  = ' rel="v:url" property="v:title"';  
  $link     = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;  
  $parent_id  = $parent_id_2 = $post->post_parent;  
  $frontpage_id = get_option('page_on_front');  
  
  if (is_home() || is_front_page()) {  
  
    if ($show_on_home == 1) echo '<div class="breadcrumbs"><a href="' . $home_link . '">' . $text['home'] . '</a></div>';  
  
  } else {  
  
    echo '<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';  
    if ($show_home_link == 1) {  
      echo '<a href="' . $home_link . '" rel="v:url" property="v:title">' . $text['home'] . '</a>';  
      if ($frontpage_id == 0 || $parent_id != $frontpage_id) echo $delimiter;  
    }  
  
    if ( is_category() ) {  
      $this_cat = get_category(get_query_var('cat'), false);  
      if ($this_cat->parent != 0) {  
        $cats = get_category_parents($this_cat->parent, TRUE, $delimiter);  
        if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);  
        $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);  
        $cats = str_replace('</a>', '</a>' . $link_after, $cats);  
        if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);  
        echo $cats;  
      }  
      if ($show_current == 1) echo $before . sprintf($text['category'], single_cat_title('', false)) . $after;  
  
    } elseif ( is_search() ) {  
      echo $before . sprintf($text['search'], get_search_query()) . $after;  
  
    } elseif ( is_day() ) {  
      echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;  
      echo sprintf($link, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F')) . $delimiter;  
      echo $before . get_the_time('d') . $after;  
  
    } elseif ( is_month() ) {  
      echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;  
      echo $before . get_the_time('F') . $after;  
  
    } elseif ( is_year() ) {  
      echo $before . get_the_time('Y') . $after;  
  
    } elseif ( is_single() && !is_attachment() ) {  
      if ( get_post_type() != 'post' ) {  
        $post_type = get_post_type_object(get_post_type());  
        $slug = $post_type->rewrite;  
        printf($link, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);  
        if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;  
      } else {  
        $cat = get_the_category(); $cat = $cat[0];  
        $cats = get_category_parents($cat, TRUE, $delimiter);  
        if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);  
        $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);  
        $cats = str_replace('</a>', '</a>' . $link_after, $cats);  
        if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);  
        echo $cats;  
        if ($show_current == 1) echo $before . get_the_title() . $after;  
      }  
  
    } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {  
      $post_type = get_post_type_object(get_post_type());  
      echo $before . $post_type->labels->singular_name . $after;  
  
    } elseif ( is_attachment() ) {  
      $parent = get_post($parent_id);  
      $cat = get_the_category($parent->ID); $cat = $cat[0];  
      $cats = get_category_parents($cat, TRUE, $delimiter);  
      $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);  
      $cats = str_replace('</a>', '</a>' . $link_after, $cats);  
      if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);  
      echo $cats;  
      printf($link, get_permalink($parent), $parent->post_title);  
      if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;  
  
    } elseif ( is_page() && !$parent_id ) {  
      if ($show_current == 1) echo $before . get_the_title() . $after;  
  
    } elseif ( is_page() && $parent_id ) {  
      if ($parent_id != $frontpage_id) {  
        $breadcrumbs = array();  
        while ($parent_id) {  
          $page = get_page($parent_id);  
          if ($parent_id != $frontpage_id) {  
            $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));  
          }  
          $parent_id = $page->post_parent;  
        }  
        $breadcrumbs = array_reverse($breadcrumbs);  
        for ($i = 0; $i < count($breadcrumbs); $i++) {  
          echo $breadcrumbs[$i];  
          if ($i != count($breadcrumbs)-1) echo $delimiter;  
        }  
      }  
      if ($show_current == 1) {  
        if ($show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id)) echo $delimiter;  
        echo $before . get_the_title() . $after;  
      }  
  
    } elseif ( is_tag() ) {  
      echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;  
  
    } elseif ( is_author() ) {  
      global $author;  
      $userdata = get_userdata($author);  
      echo $before . sprintf($text['author'], $userdata->display_name) . $after;  
  
    } elseif ( is_404() ) {  
      echo $before . $text['404'] . $after;  
    }  
  
    if ( get_query_var('paged') ) {  
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';  
      echo __('Page') . ' ' . get_query_var('paged');  
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';  
    }  
  
    echo '</div><!-- .breadcrumbs -->';  
  
  }  
} 

//function get property of agent
function zonthemes_property_of_agent($agent_id,$number){
  $args = array(
    'post_type' => 'property',
    'showposts' => $number,
    'meta_query' => array(
       array(
           'key' => 'agents',
           'value' => $agent_id,
           'compare' => 'LIKE',
       )
    )
  );
  query_posts($args);
  if(have_posts()) while(have_posts()) : the_post(); ?>
    <li>
      <div class="list-img">
        <span class="list-new">New</span>
        <a href="<?php the_permalink(); ?>" class="view">View</a>
        <a href="<?php the_permalink(); ?>">
          <img class="pro-img" src="<?php echo zon_get_thumbnail("medium"); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
        </a>
      </div>
      <p class="pro-list-title">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
      </p>
      <p class="pro-list-add"><?php echo zon_meta('zon_address'); ?></p>
      <p class="pro-list-desc"><?php echo get_the_excerpt(); ?></p>
      <p class="pro-list-price"><?php if(zon_meta("zon_lease") ==  "sale"){echo "Sale : ";}else{echo "Rent : ";} ?> <span><?php echo zon_meta("zon_price"); ?></span></p>
      <div class="pro-list-info">
        <span class="pro-list-area"><?php echo zon_meta("zon_area"); ?> sqft</span>
        <ul>
          <li class="pro-list-toilet"><?php echo zon_meta("zon_tolet"); ?></li>
          <li class="pro-list-bedroom"><?php echo zon_meta("zon_bedroom"); ?></li>
        </ul>
        <div class="clear"></div>
      </div>
      <div class="clear"></div>
    </li>
  <?php endwhile;
}
//end get property of agent.


//function get view count.
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

//function set view count.
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}


//function get post in taxonomy id.
function zon_get_tax($tax,$id,$number){
  $term = get_term( $id , $tax );
  $query = new WP_Query(array('post_type'=>'property','location'=>$term->slug,'showposts'=>$number));
  $tax_posts = $query->get_posts();
  return $tax_posts;
}

function zon_property_related(){
  
}